

package rapidp2p.backend;

import java.net.*;
import java.io.*;
/**
 *
 * @author LF
 */
public class DirectoryServer implements Runnable{
    DatagramSocket listenerSocket;
    
    String lineDelimeter = "\r\n";
    String fieldDelimeter = ",";
    byte [] receiveData = new byte[2048];
    byte [] sendData    = new byte[2048];
    DirectoryEntry [] mp3Entries = new DirectoryEntry[100];//to store mp3 entries
    
    
	public void run()
	{            
            try{//create DatagramSocket for UDP connection, bind its port for all connections
                listenerSocket = new DatagramSocket(9876);
            } catch (SocketException e) {
                System.err.println("DatagramSocket exception: " + e);
                return;
            }
            
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            try{
                listenerSocket.receive(receivePacket);
            } catch (IOException e){
                System.err.println("listenerSocket.receive() problem : " + e);
            }
            String sentence = new String(receivePacket.getData());  //from bytes to String
            System.out.println("sentence: " + sentence);
            
            //decide what type of message it is
            //Inform and Update or Query
           
            String [] lines = sentence.split(lineDelimeter);   //split String into Array of lines, each line represents single mp3 entry
            String [] entryFields = new String[3];      //array to store field of each line - 3 fields of mp3 entry
            
            for(int k=0; k<lines.length; k++){
                entryFields = lines[k].split(fieldDelimeter);
                mp3Entries[k] = new DirectoryEntry(entryFields[0], entryFields[1], entryFields[3]); 
            }
            
            InetAddress ipAddress = receivePacket.getAddress();//get incoming client's IP
            int port = receivePacket.getPort();                //get incoming client's port # 
            
            //if client is searching for mp3  - return IP of mp3 host
            String returnSentence = "this is what will be returned back to client host requesting file info";
            sendData = returnSentence.getBytes();//convert returnSentence to bytes
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipAddress, port);
            try{
            listenerSocket.send(sendPacket);
            } catch (IOException e){
                System.err.println("Error sending UDP packet" + e);
            }
                   
            System.out.println("Directory Server initialized");
            //running = true;                   //update RUNNING state  
	}
        
        
     public static void main (String [] args){
         //DatagramSocket listenerSocket;
         //DirectoryServer ds = new DirectoryServer();
         //ds.start();                                    
         //starts new Thread, since main class extends Thread
         while(true){
           new Thread(new DirectoryServer()).start(); 
         }
     }
     
}
